package com.supor.aiot.food.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.supor.aiot.food.R;
import com.supor.aiot.food.bean.Children;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


public class ClassifyDeviceAdapter extends RecyclerView.Adapter<ClassifyDeviceAdapter.ClassifyGoodsHolder>{
    private List<Children> mList;
    private String parentPicture;
    private Context mContext;
    private OnClassifyClickListener recipeDeviceClick;
    public ClassifyDeviceAdapter(List<Children> mList,Context mContext , OnClassifyClickListener listener) {
        this.mList = mList;
        this.mContext = mContext;
        this.recipeDeviceClick = listener;
        this.parentPicture = parentPicture;
    }

    @NonNull
    @Override
    public ClassifyDeviceAdapter.ClassifyGoodsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_classify_device,parent, false);
        return new ClassifyDeviceAdapter.ClassifyGoodsHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ClassifyGoodsHolder holder, int position) {
        Children deviceBean = mList.get(position);
        parentPicture=deviceBean.getModelPicture();
        holder.llGoodsOuter.setOnClickListener(view -> recipeDeviceClick.OnClassifyClick(position));

        if (!TextUtils.isEmpty(parentPicture))Glide.with(mContext).load(parentPicture).into(holder.ivGoodsImg);
        if (!TextUtils.isEmpty(deviceBean.getCrmModelName())){
            if (deviceBean.getCrmModelName().length() > 10){
                String name = deviceBean.getCrmModelName();
                int split = name.indexOf("(");
                if (split != -1) {
                    name = name.substring(0, split) + "\n" + name.substring(split);
                }
                holder.tvGoodsName.setText(name);
            }else {
                holder.tvGoodsName.setText(deviceBean.getCrmModelName());
            }
        }
//        if (!TextUtils.isEmpty(deviceBean.getModelTypeName())&&!TextUtils.isEmpty(deviceBean.getModelName())){
//            if (deviceBean.getModelTypeName().length()+deviceBean.getModelName().length()>8){
//                holder.tvGoodsName.setText(deviceBean.getModelTypeName()+"\n"+deviceBean.getModelName());
//            }else {
//                holder.tvGoodsName.setText(deviceBean.getModelTypeName()+deviceBean.getModelName());
//            }
//        }
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class ClassifyGoodsHolder extends RecyclerView.ViewHolder{
        private ImageView ivGoodsImg;
        private LinearLayout llGoodsOuter;
        private TextView tvGoodsName;
        public ClassifyGoodsHolder(@NonNull View itemView) {
            super(itemView);
            ivGoodsImg=itemView.findViewById(R.id.iv_device_img);
            llGoodsOuter=itemView.findViewById(R.id.ll_device_outer);
            tvGoodsName=itemView.findViewById(R.id.tv_device_name);
        }
    }

    public interface OnClassifyClickListener {
        void OnClassifyClick(int position);
    }

}
